/*
  Created on Jul 6, 2005
  (C) 2005 
  @author Michael Long, Adam D. Bradley

  This program is free software; you can redistribute it and/or
  modify it under the terms of Version 2 of the GNU General Public 
  License.
  
  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.
  
  You should have received a copy of the GNU Library General Public
  License along with this library; if not, write to the Free
  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

package snobject;

import org.w3c.dom.*;
import snobject.snObject;

public class snCommand extends snObject {
	public static String CMD_CLEAR = "clear";
	public static String CMD_WANT ="want";
	public static String CMD_DONTWANT = "dontwant";
	public static String CMD_EXFAIL = "exfail";
	public static String CMD_EXDONE = "exdone";
	public static String CMD_OK = "ok";
	public static String CMD_FAILED = "failed";

	
	private String param=null;

	public snCommand(String sCmd, String sParam) {
		super();
		
		this.content = sCmd;
		param = sParam;
	}

	public snCommand(String sCmd) {
		this.content = sCmd;
	}
		
	public String getArg(){
		return param;
	}
	
	public String getTypeOfCommand(){
		return (String)this.content;
	}
	
	public String getCommand() { return (String) content; }

	public String getXmlMimeType() { return MimeTypes.COMMAND; }
	public String getWebMimeType() { return "text/plain"; }
	public String getBinaryMimeType() { return "text/plain"; }
	
	public static snObject CLEAR = new snCommand("clear");
	public static snObject WANT = new snCommand("want");
	public static snObject DONTWANT = new snCommand("dontwant");

	public static snObject EXFAIL = new snCommand("exfail");
	
	public static snObject EXDONE(String sGraphName){
		return new snCommand("exdone",sGraphName);
	}

	public static snObject OK = new snCommand("ok");
	public static snObject FAILED = new snCommand("failed");

	public void xmlWrite(java.io.OutputStream out, int depth) throws java.io.IOException {
		xmlWritePre(out,depth);
		if(param==null){
			out.write(("<" + ((String)this.content) + 
				"/>").getBytes());
		}
		else{
			out.write(("<" + ((String)this.content) ).getBytes());
			out.write((" param=\"" + param + "\"/>").getBytes());
		}
		xmlWritePost(out,depth);
	}
	public void webWrite(java.io.OutputStream out, int depth) throws java.io.IOException {
		step.Node.Indent(out, depth);
		out.write(((String)this.content).getBytes());
	}
	public void binaryWrite(java.io.OutputStream out) throws java.io.IOException {
		webWrite(out,0);
	}

	public static snCommand xmlParse(Element e) {
		if (!e.getTagName().equals("snobject"))
			return null;
		Node n = e.getFirstChild();
		do {
			if (n.getNodeType() == Node.ELEMENT_NODE){
				if(n.getAttributes()!=null){
					if(n.getAttributes().getNamedItem("param")!=null)
						return new snCommand(((Element) n).getTagName(),n.getAttributes().getNamedItem("param").getNodeValue());
				}
				return new snCommand(((Element) n).getTagName());
			}
		} while ((n = n.getNextSibling()) != null);
		return null;
	}
}
